
import * as R from 'ramda'
import { Message } from 'element-ui'

export function deepClone(obj) {
  return JSON.parse(JSON.stringify(obj))
}
export const diff = R.curry(function(target, a, b) {
  if (!target) return a - b
  return a[target] - b[target]
})

export const diffIndex = diff('index')

export function filterZtreeDataByType(data, type) {
  if (!Array.isArray(data)) {
    data = data.children || []
  }
  data.forEach(item => {
    if (item.type === type - 1) {
      item.children = []
    } else {
      filterZtreeDataByType(item.children, type)
    }
  })
}

export function $msg(message = '0_操作成功', callback, duration = 1500) {
  const arr = ['success', 'error', 'info']
  const dealArr = message.split('_')
  const type = arr[~~dealArr[0]] || 'info'
  message = dealArr[1] || '未知信息'
  Message({
    type,
    showClose: true,
    message,
    duration
  })
  if (typeof callback === 'function') callback(true)
  return Promise.resolve(true)
}

export function openNewTab(url) {
  window.open(url, '_blank')
}

export function setStorage(key, value, type = 'local') {
  window[type + 'Storage'].setItem(key, value)
}

export function mixinObjs(...objs) {
  return Object.assign.apply(null, objs)
}

export const isSame = R.curry(function(target, a, b) {
  if (!target) return a === b
  return a[target] === b[target]
})

export function reDup(array) {
  return Array.from(new Set(array))
}
